//
//  ViewController.m
//  AddressList
//
//  Created by dllo on 15/12/10.
//  Copyright © 2015年 尹凯. All rights reserved.
//

#import "ViewController.h"
#import "CustomTableViewCell.h"
#import "AppTool.h"

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *myTableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.myTableView];
    [self.myTableView registerClass:[CustomTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(50, 50, 50, 50);
    button.backgroundColor = [UIColor blueColor];
    [button setTitle:@"点我" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(haha) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

- (void)haha{
    NSLog(@"哈哈");

    [[AppTool shareAppTool] insertStudentName:@"马强"];
    [self.myTableView reloadData];
}

- (UITableView *)myTableView{
    if (_myTableView == nil) {
        _myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
        _myTableView.dataSource = self;
        _myTableView.delegate = self;
    }
    return _myTableView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    NSMutableDictionary *dictionary = [[AppTool shareAppTool] selectStudent];
    return dictionary.allKeys.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSMutableDictionary *dictionary = [[AppTool shareAppTool] selectStudent];
    NSString *key = [dictionary.allKeys objectAtIndex:section];
    NSMutableArray *array = [dictionary objectForKey:key];
    return array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"cell";
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    NSMutableDictionary *dictionary = [[AppTool shareAppTool] selectStudent];
    NSString *key = [dictionary.allKeys objectAtIndex:indexPath.section];
    NSMutableArray *array = [dictionary objectForKey:key];
    Student *student = [array objectAtIndex:indexPath.row];
    cell.textLabel.text = student.name;
    
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
